<template>
    <svg :class="svgClass" :style="styles" aria-hidden="true" @click="handleClick">
        <use :xlink:href="iconName"></use>
    </svg>
</template>

<script>
    export default {
        name: 'CIcon',
        props: {
            name: {
                type: String,
                required: true
            },
            className: String,
            size: {
                type: [Number, String],
                default: 12,
            },
            color: String,
        },
        computed: {
            iconName() {
                return `#icon-${this.name}`;
            },
            svgClass() {
                if (this.className) {
                    return 'c-icon ' + this.className;
                }
                return 'c-icon';
            },
            styles () {
                let style = {
                    'font-size': `${this.size}px`,
                    'width': `${this.size}px`,
                    'height': `${this.size}px`,
                };

                if (this.color) {
                    style['fill'] = this.color;
                }

                return style;
            }
        },
        methods: {
            handleClick(event) {
                this.$emit('click', event);
            }
        }
    };
</script>

<style lang="less">
    .c-icon {
        vertical-align: -0.15em;
        fill: currentColor;
        overflow: hidden;

        use {
            pointer-events: none;
        }
    }
</style>
